package org.lora.biz.workflow;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.lora.biz.workflow.engines.IProcessDefinitionService;
import org.lora.core.annotation.Action;
import org.lora.core.annotation.AutoAssemble;
import org.lora.mvc.annotation.RequestPath;
import org.lora.mvc.annotation.ResponseJSON;
import org.lora.mvc.entity.ViewData;
import org.lora.webframework.common.WebframeBaseAction;

/**
 * <p>
 * 类名:ProcessDefinitionAction
 * </p>
 * 描述:<br>
 * 创建时间:2016年4月11日 下午5:28:29<br>
 * 
 * @author Bladnir@outlook.com<br>
 *         修改历史:<br>
 *         2016年4月11日 Bladnir 创建<br>
 */
@Action
@RequestPath(path = "/biz/workflow/processDefinition")
public class ProcessDefinitionAction extends WebframeBaseAction {

	/**
	 * 流程定义服务
	 */
	@AutoAssemble
	private IProcessDefinitionService processDefinitionService;

	/**
	 * 打开流程定义设计页面
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestPath(path = "/showDefinition")
	public ViewData showDefinition() {
		return new ViewData("/jsp/workflow/definition.jsp");
	}

	/**
	 * 获取流程定义zTree数据
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestPath(path = "/getBodefTreeData")
	@ResponseJSON
	public Object getBodefTreeData(HttpServletRequest request) {

		// 域ID
		String domainId = request.getParameter("domainId");

		List<Map<String, Object>> resultList = new ArrayList<>();

		try {
			resultList = processDefinitionService.getProcessDefinitionTreeData(domainId);
		} catch (Exception e) {
			businessLogger.error(e.getMessage(), e);
		}

		return resultList;
	}

	/**
	 * 报错流程定义信息到数据库
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestPath(path = "/saveProcessDefinition")
	@ResponseJSON
	public Object saveProcessDefinition(HttpServletRequest request) {

		String version = request.getParameter("version");
		String processDefinitionId = request.getParameter("processDefinitionId");
		String resourceId = request.getParameter("resourceId");
		String flowData = request.getParameter("flowData");

		try {
			processDefinitionService.saveFlowResourceData(version, processDefinitionId, resourceId, flowData);
		} catch (Exception e1) {
			businessLogger.error(e1.getMessage(), e1);
		}

		return null;
	}

	/**
	 * 获取流程定义字符串
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestPath(path = "/getProcessResource")
	@ResponseJSON
	public Object getProcessResource(HttpServletRequest request) {

		String resourceId = request.getParameter("resourceId");

		String result = "";

		try {
			result = processDefinitionService.getFlowResource(resourceId);
		} catch (Exception e1) {
			businessLogger.error(e1.getMessage(), e1);
		}

		return result;

	}

}
